package com.cmbchina.ccd.utils;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @Description:
 * @date 2021/10/24 12:25
 */
public class GenerateUtil {

    public static void main(String[] args) {
        genterator("jdbc:mysql://47.100.242.231:3306/adigital_translate?autoReconnect=true&useUnicode=true&characterEncoding=utf8",
                "root",
                "admin123",
                "D:\\idea2020\\opensource\\smart-report\\smart-report-back\\src\\main\\java",
                new String[]{"report_data","report_activity","report_activity_words"},
                "com.cmbchina.ccd.db");
    }

    public static void genterator(String url, String username, String password,
                                  String outputDir, String[] tables, String packageParent) {
        //配置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUrl(url);
        dsc.setUsername(username);
        dsc.setPassword(password);

        //设置一些全局的配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(outputDir);
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setAuthor("scmpt");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");

        //生成策略配置
        StrategyConfig strategy = new StrategyConfig();
        //strategy.setTablePrefix(new String[] { "SYS_" });// 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setInclude(tables); // 需要生成的表

        //生成文件所在包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(packageParent);
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setXml("mapper.xml");

        AutoGenerator mpg = new AutoGenerator();
        mpg.setDataSource(dsc);         //数据源配置
        mpg.setGlobalConfig(gc);        //全局配置
        mpg.setStrategy(strategy);      //生成策略配置
        mpg.setPackageInfo(pc);         //包配置
        mpg.execute();
    }
}
